package com.steven.nio;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Set;

public class NIOClient {
    public static void main(String[] args) throws Exception {
        //创建SocketChannel;
        SocketChannel socketChannel = SocketChannel.open();
        //设置为非阻塞
        socketChannel.configureBlocking(false);
        InetSocketAddress localhost = new InetSocketAddress("localhost", 7777);
        //连接服务器
        if(!socketChannel.connect(localhost)){
            while (!socketChannel.finishConnect()){
                System.out.println("连接需要时间，非阻塞");
            }
        }
        //连接成功发送数据
        String str ="你好我是客户端";
        ByteBuffer wrap = ByteBuffer.wrap(str.getBytes());
        //发送数据
        socketChannel.write(wrap);
        System.in.read();
    }
}
